package cn.victory._04_redisCommands;

import io.lettuce.core.RedisClient;
import io.lettuce.core.RedisURI;
import io.lettuce.core.api.StatefulRedisConnection;
import org.junit.Test;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

/**
 * @ Description TODO
 * @ Author victory
 * @ Date 2022/6/29
 **/
public class redisCommandsDemo {

    @Test
    public void testRedisCommandsSync(){
        //创建URI
        RedisURI redisUri = RedisURI.builder().withHost("localhost").withPort(6379).build();
        //创建客户端
        RedisClient redisClient = RedisClient.create(redisUri);
        //连接
        StatefulRedisConnection<String, String> connect = redisClient.connect();
        //操作连接(同步)
        connect.sync().set("user", "victory");
        //拿取结果
        String user = connect.sync().get("user");
        System.out.println(user);
        //关闭连接
        connect.close();
        //关闭客户端
        redisClient.shutdown();
    }

    @Test
    public void testRedisCommandsAsync() throws InterruptedException, ExecutionException, TimeoutException {
        //创建URI
        RedisURI redisUri = RedisURI.builder().withHost("localhost").withPort(6379).build();
        //创建客户端
        RedisClient redisClient = RedisClient.create(redisUri);
        //连接
        StatefulRedisConnection<String, String> connect = redisClient.connect();
        //操作连接(异步)
        connect.async().set("user", "victory2");
        //拿取结果最多等待1秒
        String user = connect.async().get("user").get(1, TimeUnit.SECONDS);
        System.out.println(user);
        //关闭连接
        connect.close();
        //关闭客户端
        redisClient.shutdown();
    }



    @Test
    public void testRedisCommands(){
        //创建URI
        RedisURI redisUri = RedisURI.builder().withHost("localhost").withPort(6379).build();
        //创建客户端
        RedisClient redisClient = RedisClient.create(redisUri);
        //连接
        StatefulRedisConnection<String, String> connect = redisClient.connect();
        //操作连接(响应式)
        connect.reactive().set("user", "victory");

        //*******************************************TODO  响应式Reactive学习
        connect.reactive().get("user");
        //关闭连接
        connect.close();
        //关闭客户端
        redisClient.shutdown();
    }
}
